package com.linjj.seckill.rabbitmq;

import com.linjj.seckill.config.RabbitMQConfig;
import com.linjj.seckill.domain.User;
import com.linjj.seckill.redis.RedisService;
import com.linjj.seckill.service.IGoodsService;
import com.linjj.seckill.service.ISeckillService;
import com.linjj.seckill.vo.SeckillGoodsVo;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * 消息消费者
 * 减库存，创建订单
 * */
@Service
public class MessageReceiver {
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private ISeckillService seckillService;
    @RabbitListener(queues = RabbitMQConfig.SECKILL_QUEUE)
    public void createOrder(String message){
        SeckillMessage seckillMessage = RedisService.stringToBean(message,SeckillMessage.class);
        if(seckillMessage!=null){
            User user = seckillMessage.getUser();
            long goodsId = seckillMessage.getGoodsId();
            String address = seckillMessage.getAddress();
            SeckillGoodsVo seckillGoodsVo = goodsService.getSeckillVoByGoodsId(goodsId);
            int stock = seckillGoodsVo.getSeckillStock();
            if(stock <= 0) {
                return;
            }
           seckillService.seckill(user,seckillGoodsVo,address);
        }
    }
}
